package com.project.hqshorttaxi.controller;

import com.github.pagehelper.PageHelper;
import com.project.hqshorttaxi.aop.TagLog;
import com.project.hqshorttaxi.dto.BaseParam;
import com.project.hqshorttaxi.dto.CameraParam;
import com.project.hqshorttaxi.entity.Camera;
import com.project.hqshorttaxi.entity.Setting;
import com.project.hqshorttaxi.service.CameraService;
import com.project.hqshorttaxi.service.SettingService;
import com.project.hqshorttaxi.util.PageInfo;
import com.project.hqshorttaxi.util.RestResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/setting/")
public class SettingController {

    @Resource
    SettingService settingService;

    /**
     * 新增
     */
    @PostMapping("add")
    @TagLog(operation = 2, content = "新增基础参数")
    public RestResult add(@RequestBody Setting param) {
        try {
            settingService.insert(param);
            return RestResult.ok();
        } catch (Exception e) {
            return RestResult.error(e.getMessage());
        }
    }

    /**
     * 更新
     */
    @PostMapping("update")
    @TagLog(operation = 3, content = "更新基础参数")
    public RestResult update(@RequestBody Setting param) {
        try {
            Setting entity = settingService.getById(param.getId());
            if (entity == null) {
                return RestResult.error("该条数据不存在");
            } else {
                settingService.update(param);
                return RestResult.ok();
            }
        } catch (Exception e) {
            return RestResult.error(e.getMessage());
        }
    }

    /**
     * 删除
     */
    @PostMapping("delete/{id}")
    @TagLog(operation = 4, content = "删除基础参数")
    public RestResult delete(@PathVariable Integer id) {
        try {
            Setting entity = settingService.getById(id);
            if (entity == null) {
                return RestResult.error("该条数据不存在");
            } else {
                settingService.delete(id);
                return RestResult.ok();
            }
        } catch (Exception e) {
            return RestResult.error(e.getMessage());
        }
    }

    /**
     * 列表
     */
    @PostMapping("page")
    @TagLog(operation = 0, content = "查询基础参数列表")
    public RestResult page(@RequestBody BaseParam param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        return RestResult.ok(new PageInfo<>(settingService.list()));
    }

    /**
     * 根据ID获取
     */
    @GetMapping("{id}")
    @TagLog(operation = 0, content = "根据ID获取基础参数详细信息")
    public RestResult pageById(@PathVariable Integer id) {
        return RestResult.ok(settingService.getById(id));
    }

}
